package com.lexian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lexian.entity.*;
import com.lexian.entity.vo.CartGoodsVo;
import com.lexian.entity.vo.GoodsStoreVo;
import com.lexian.entity.vo.GoodsVo;
import com.lexian.mapper.GoodsMapper;
import com.lexian.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lexian.utils.CommonResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-14
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsattrService goodsattrService;
    @Autowired
    private CartService cartService;
    @Autowired
    private CartitemService cartitemService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private GoodsstoreService goodsstoreService;


    //获取所有商品信息
    @Override
    public List<GoodsVo> getInfo() {
        List<GoodsVo> finaList = new ArrayList<>();

        //查询商品表所有信息
        List<Goods> goodsList = baseMapper.selectList(null);

        //遍历商品集合吗，根据商品类别id查出商品类别名称，并根据商品id查出商品属性，并添加到List<GoodsVo>集合中
        for (Goods goods : goodsList) {
            GoodsVo goodsVo = new GoodsVo();
            //将遍历的基础商品集合复制到业务商品信息集合中
            BeanUtils.copyProperties(goods,goodsVo);
            //根据商品类别id查出商品类别名称
            Category category = categoryService.getById(goods.getCategoryId());
            if(category != null){
                goodsVo.setSubjectName(category.getName());
            }
            //据商品id查出商品属性
            QueryWrapper<Goodsattr> queryWrapper = new QueryWrapper<>();
            //设置条件，属性表中商品id列的值为goods.getGoodsId()
            queryWrapper.eq("goods_id",goods.getGoodsId());
            List<Goodsattr> goodsattrList = goodsattrService.list(queryWrapper);
            //将多个属性存入业务商品对象
            goodsVo.setGoodsattrList(goodsattrList);
            //将业务对象加入到返回的集合中
            finaList.add(goodsVo);
        }
        return finaList;
    }

    /**
     * 获取购物车商品
     * @param cusId
     * @return
     */
    public List<CartGoodsVo> getCartGoods(long cusId){
        //购物车的商品信息
        List<CartGoodsVo> CartGoodslist=new ArrayList<>();
        //购物车所有商品的id
        List goodsIdList=new ArrayList<>();
        long cartId=cartService.getCartIdByCusId(cusId);
        //查询出跟购物车联系的所有的商品的id以及数量
        List<Cartitem> Cartitemlist=cartitemService.CartGoods(cartId);
       //遍历获取所有商品id
        for (Cartitem cartitem:Cartitemlist){
            goodsIdList.add(cartitem.getGoodsId());
        }
        //根据商品id查询商品表所有信息
        List<Goods> goodsList = goodsService.getGoodsByGoodsId(goodsIdList);
        for (Cartitem cartitem:Cartitemlist){
            for (Goods goods : goodsList){
                CartGoodsVo cartGoodsVo=new CartGoodsVo();
                //将遍历的基础商品集合复制到业务商品信息集合中
                BeanUtils.copyProperties(goods,cartGoodsVo);
                cartGoodsVo.setNum(cartitem.getNum());
                CartGoodslist.add(cartGoodsVo);
            }
        }
        return CartGoodslist;
    }

<<<<<<< HEAD
    //获取门店所有的商品信息
    @Override
    public List<GoodsStoreVo> getByInfo(Long gid) {
        //门店的商品信息
        List<GoodsStoreVo> gslist = new ArrayList<>();
        List gsIdList = new ArrayList<>();
        long storeId = storeService.getStoreIdBy(gid);
        //查询出跟门店联系的所有的商品的id以及库存数量
        QueryWrapper<Goodsstore> wrapper = new QueryWrapper<>();
        wrapper.select("goodsstore_id","goodsAmont").eq("store_id",storeId);
        List<Goodsstore> GoodsstoreList = goodsstoreService.list(wrapper);
        //遍历获取所有商品id
        for (Goodsstore goodsstore:GoodsstoreList){
            gsIdList.add(goodsstore.getGoodsId());
        }
        QueryWrapper<Goods> wrapperone=new QueryWrapper<>();
        wrapperone.eq("goods_id",gsIdList);
        //根据商品Id查询商品表所有信息
        List<Goods> goodsList = baseMapper.selectList(wrapperone);
        for (Goodsstore goodsstore:GoodsstoreList){
            for (Goods goods:goodsList){
                GoodsStoreVo goodsStoreVo = new GoodsStoreVo();
                //将遍历的基础商品集合复制到业务商品信息集合中
                BeanUtils.copyProperties(goods,goodsStoreVo);
                goodsStoreVo.setGoodsAmont(goodsstore.getGoodsAmont());
                gslist.add(goodsStoreVo);
            }
        }
        return gslist;
=======
    /**
     * 通过list集合里的商品ID查询所有对应商品
     * @param goodsIdList 商品ID集合
     * @return
     */
    public List<Goods> getGoodsByGoodsId(List goodsIdList){
        QueryWrapper<Goods> wrapperone=new QueryWrapper<>();
        wrapperone.eq("goods_id",goodsIdList);
        return baseMapper.selectList(wrapperone);
>>>>>>> a1e4429361f088c5d2c38576e7d531c8ac58ca25
    }
}
